<?php 
namespace Admin\Controller;
use \Admin\Controller\AdminController;

class TaskController extends AdminController
{
	/**
	 * 任务类型列表
	 * @author 刘新
	 */
	public function taskList()
	{
		$con = I('condition');
		$where['status'] = array('egt', 0);
		
		//查询条件
		if($con) {
			if(preg_match('/^[1-9]\d*$/', $con)) {
				$where['id'] = $con;
			}
		}
		
		//获取任务列表
		$list = $this->lists('Task', $where);
		
		if($list) {
			$typeIds = array();
			
			//获取任务类型ID
			foreach($list as $val) {
				$typeIds[] = $val['type_id'];
			}
			
			//查询任务类型信息
			$typeIds = array_unique($typeIds);
			$type = M('TaskType')->where(array('id' => array('in', $typeIds), 'status' => 1))->select();
			!$type && $this->error('任务类型查询失败');
			//获取任务类型ID
			foreach($type as $v) {
				foreach($list as &$val) {
					if($v['id'] === $val['type_id']) {
						$val['type_status'] = $v['status'];
						$val['type_name'] = $v['name'];
					}
				}
			}
			
			int_to_string($list, array('type_status'=>array(1=>'正常',-1=>'删除',0=>'禁用')));
		}
		
		int_to_string($list);
		//die(json_encode($list));
		$this->assign('_list', $list);
		$this->display('task_list');
	}
	
	/**
	 * 添加任务
	 * @author 刘新
	 */
	public function addTask()
	{
		if(IS_GET) {
			$typeList = M('TaskType')->where('status>-1')->select();
			!$typeList && $this->error('任务类型获取失败');
			$this->assign('_title', '添加任务');
			$this->assign('fileTypeList', C('APP_FILE')[0]);
			$this->assign('taskType', $typeList);
			$this->assign('fileids', "0");
			$this->display('add_edit');
		}
	
		//添加任务类型
		$res = D('Task')->addTask();
		if($res['error'] !== 0) $this->error($res['msg']);
		$info = $_POST['description'];
		preg_match_all("/(font-size:)(.*?)(px;)/",$info,$str);
		foreach($str[0] as $key=>$val){
			preg_match("/(font-size:)(.*?)(px;)/",$val,$i);
			if($i[2]<20){
				$info = str_replace($i[0],"font-size:20px;",$info);
			}
		}
		$info = '<meta charset="utf8"><style>  h1{ font-size:30px; } h2{ font-size:25px; } h3{ font-size:20px; } </style>'.$info;
		filewrite("./Public/static/active/activeinfo".$_POST['type_id'].".html",$info);

		$this->success('添加成功', U('Task/taskList'));
	}

	/**
	 * 编辑任务类型
	 * @author 刘新
	 */
	public function editTask()
	{
		$id = I('id', '', '/^[1-9]\d*$/');
		!$id && $this->error('请选择要编辑的任务');
	
		//如果是GET请求，则导入编辑界面
		if(IS_GET) {
			//查询任务类型
			$taskType = D('TaskType')->where(array('status' => 1))->select();
			!$taskType && $this->error('任务类型查询失败');
			//查询要编辑的任务
			$task = M('Task')->where(array('id' => $id))->find();
			!$task && $this->error('任务查询失败');
			$fileids = explode(",",$task['file_ids']);
			//查询任务关联文件类型
			$id = split(',', $task['file_ids'])[0];
			$fileType = false;
			if($id) $fileType = M('File')->where(array('id' => $id))->getField('file_type');
			//dump($fileType);die($fileType);
			$this->assign('taskType', $taskType);
			$this->assign('fileType', $fileType);
			$this->assign('task', $task);
			$this->assign('fileids', json_encode($fileids));
			$this->assign('fileTypeList', C('APP_FILE')[0]);
			$this->assign('_title', '编辑任务');
			$this->display('add_edit');
		}

		//编辑任务类型
		$res = D('Task')->editTask();
		if($res['error'] != 0) $this->error($res['msg']);
		$info = $_POST['description'];
		preg_match_all("/(font-size:)(.*?)(px;)/",$info,$str);
		foreach($str[0] as $key=>$val){
			preg_match("/(font-size:)(.*?)(px;)/",$val,$i);
			if($i[2]<20){
				$info = str_replace($i[0],"font-size:20px;",$info);
			}
		}
		$info = '<meta charset="utf8"><style>  h1{ font-size:30px; } h2{ font-size:25px; } h3{ font-size:20px; } </style>'.$info;
		filewrite("./Public/static/active/activeinfo".$_POST['type_id'].".html",$info);

		$this->success('编辑成功', U('Task/taskList'));
	}
	
	/**
	 * 更新任务状态
	 * @author 刘新
	 */
	public function changeStatus($method=null)
	{
		$id = array_unique((array)I('id',0));
		$id = is_array($id) ? implode(',',$id) : $id;
		if ( empty($id) ) $this->error('请选择要操作的数据!');
		$map['uid'] =   array('in',$id);
	
		switch ( strtolower($method) )
		{
			case 'forbid':
				$this->forbid('Task', $map );
				break;
			case 'resume':
				$this->resume('Task', $map );
				break;
			case 'delete':
				$this->delete('Task', $map );
				break;
			default:
				$this->error('参数非法');
		}
	}
	
	/**
	 * 任务详情
	 * @author 刘新
	 */
	public function taskDetail()
	{
		$id = I('id', '', '/^[1-9]\d*$/');
		!$id && $this->error('请输入任务ID');
		
		//查询任务
		$detail = D('Task')->find($id);
		!$detail && $this->error('查询失败');
		
		//查询任务类型
		$type = M('TaskType')->where('id=' . $detail['type_id'])->find();
		!$type && $this->error('任务类型查询失败');
		$detail['type_name'] = $type['name'];
		
		$detail = array($detail);
		int_to_string($detail);
		$this->assign('detail', $detail[0]);
		$this->display('task_detail');
	}
	
	/**
	 * 任务类型列表
	 * @author 刘新
	 */
	public function taskTypeList()
	{
		$con = I('condition');
		$where['status'] = array('egt', 0);
		
		if($con) {
			if(preg_match('/^[1-9]\d*$/', $con)) {
				$where['id'] = $con;
			} else if(preg_match('/^\w+$/', $con)) {
				$where['code'] = $con;
			}
		}
		
		$list = $this->lists('TaskType', $where);
		int_to_string($list);
		$this->assign('_list', $list);
		$this->display('type_list');
	}
	
	/**
	 * 添加任务类型
	 * @author 刘新
	 */
	public function addTaskType()
	{
		if(IS_GET) {
			$this->assign('_title', '添加任务类型');
			$this->display('add_edit_type');
		}

		//添加任务类型
		$res = D('TaskType')->addTaskType();

		if($res['error'] !== 0) $this->error($res['msg']);


		$this->success('添加成功', U('Task/taskTypeList'));
	}

	/**
	 * 编辑任务类型
	 * @author 刘新
	 */
	public function editTaskType()
	{
		$id = I('id', '', '/^[1-9]\d*$/');
		!$id && $this->error('请选择要编辑的任务类型');
	
		//如果是GET请求，则导入编辑界面
		if(IS_GET) {
			$taskType = D('TaskType')->where(array('id' => $id))->find();
			!$taskType && $this->error('查询失败，请确认要编辑的任务类型是否存在');
			$this->assign('_title', '编辑任务类型');
			$this->assign('taskType', $taskType);
			$this->display('add_edit_type');
		}
	
		//编辑任务类型
		$res = D('TaskType')->editTaskType();


		$this->success('编辑成功', U('Task/taskTypeList'));
	}
	
	/**
	 * 任务类型详情
	 * @author 刘新
	 */
	public function taskTypeDetail()
	{
		$id = I('id', '', '/^[1-9]\d*$/');
		!$id && $this->error('请输入任务类型ID');
		$detail = D('TaskType')->find($id);
		!$detail && $this->error('查询失败');
		$detail = array($detail);
		int_to_string($detail);
		$this->assign('detail', $detail[0]);
		$this->display('task_type_detail');
	}
	
	/**
	 * 更新任务类型状态
	 * @author 刘新
	 */
	public function changeTypeStatus($method=null)
	{
		$id = array_unique((array)I('id',0));
		$id = is_array($id) ? implode(',',$id) : $id;
		if ( empty($id) ) $this->error('请选择要操作的数据!');
		$map['uid'] =   array('in',$id);
		
		switch ( strtolower($method) )
		{
			case 'forbid':
				$this->forbid('TaskType', $map );
				break;
			case 'resume':
				$this->resume('TaskType', $map );
				break;
			case 'delete':
				$this->delete('TaskType', $map );
				break;
			default:
				$this->error('参数非法');
		}
	}
	
	/**
	 * 删除任务
	 * @author 刘新
	 */
	public function clearTask()
	{
		$ids = I('id');
		if(empty($ids)) $this->error('请选择要清空的数据');
		if(!is_array($ids)) $ids = (array)$ids;
		$res = M('Task')->where(array('id' => array('in', $ids)))->delete();
		!$res && $this->error('清空失败');
		$this->success('清空成功');
	}
	
	/**
	 * 任务相关文件
	 * @author 刘新
	 */
	public function taskFile()
	{
		$taskTypeId = I('task_type_id', '', '/^\d+$/');
		if(!$taskTypeId) $this->ajaxReturn(array('error' => 1, 'msg' => '参数错误'));
		$taskType = $this->returnOne('TaskType', array('id'=>$taskTypeId), '', 'code');
		if(!$taskType) $this->ajaxReturn(array('error' => 1, 'msg' => '查无数据'));
		
		//判断文件类型
		$code = $taskType['code'];
		if($code == 'banner') {
			$type = 1;
		} else if($code == 'recommend') {
			
		} else if($code == 'register') {
			
		}
		if(!$type) $this->ajaxReturn(array('error' => 1, 'msg' => '任务类型获取失败'));
		
		//获取文件信息
		$file = $this->returnList('File', array('type_id' => $type), '', 'id,name,url');
		if(!$file) $this->ajaxReturn(array('error' => 1, 'msg' => '查询失败'));
		
		//如果传递任务id，则获取已被关联的文件id
		$task = $this->returnOne('Task', array('id' => I('task_id')));
		if($task && $task['file_ids']) {
			$selected = explode(',', $task['file_ids']);
			foreach($file as $key => $val) {
				if(in_array($val['id'], $selected)) $file[$key]['selected'] = true;
			}
		}
		$this->ajaxReturn(array('error' => 0, 'msg' => $file));
	}
}
